package cn.ekuma.epos.module;


import java.util.ArrayList;
import java.util.List;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.I_Session;
import com.openbravo.pos.base.AppConfig;
import com.openbravo.pos.base.BeanFactoryException;

import e.odbo.data.dao.I_DataLogic;
import cn.ekuma.epos.datalogic.I_DataLogicAdmin;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicReceipts;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.I_ProcessManager;
import cn.ekuma.epos.security.OldPOSSecurityManager;
import cn.ekuma.rabbit.ModelManager;
import cn.ekuma.rabbit.imp.ConfigModel;
import cn.ekuma.rabbit.spi.I_Model;
import cn.ekuma.rabbit.spi.I_ModelCommand;
import cn.ekuma.security.I_SecurityManager;
import cn.ekuma.security.datalogic.I_DataLogicSecurity;

public abstract class AbstractPosModel implements I_Model{

	protected ModelManager manager;
	protected boolean able=false;
	protected CoreModel coreModel;
	protected ConfigModel configModel;
	
	protected  I_DataLogicERP dlERP;
	protected  I_DataLogicSales dlSales;
	protected  I_DataLogicReceipts dlReceipts;
	protected  I_DataLogicSystem dlSystem;
	protected  I_DataLogicAdmin dlAdmin;
	protected  I_DataLogicCustomers dlCustomers;
	protected  I_DataLogicSecurity dlSecurity;

	I_SecurityManager securityManager;
	
	protected I_ProcessManager processManager;

	protected I_Session session;
	@Override
	public void init(ModelManager manager) throws BasicException {
		this.manager=manager;
		
		coreModel=(CoreModel) manager.getModelByName(CoreModel.NAME);
		configModel=(ConfigModel) manager.getModelByName(ConfigModel.NAME);
		able=true;
		String linkModel=configModel.getProperty(AppConfig.LINK_MODE);
		if("false".equalsIgnoreCase(linkModel)){
            DefaultDataLogicModel	model= (DefaultDataLogicModel) manager.getModelByName(DefaultDataLogicModel.NAME);
            dlSystem = model.getDLSystem();
            dlERP=model.getDLERP();
            dlSales=(I_DataLogicSales) dlERP;
            dlAdmin=model.getDLAdmin();
            dlCustomers=model.getDLCustomer();		
            dlReceipts=model.getDLReceipts();
            dlSecurity=model.getDLSecurity();
            processManager=model.processManager;
            this.session=model.s;
		}
	
	}

	public I_Session getSession(){
         return session;
	}

	@Override
	public boolean isAble() {
		return able;
	}

	@Override
	public List<String> getModelDependents() {
		List<String> ret=new ArrayList<String>();
		ret.add(CoreModel.NAME);
		return ret;
	}

	@Override
	public Object getBean(String beanfactory) throws BeanFactoryException {
		return coreModel.getBean(beanfactory);
	}
	
	@Override
	public I_SecurityManager getModelSecurityManager() {
		if(securityManager==null)
			securityManager= OldPOSSecurityManager.getInstance(dlSecurity,dlSystem);
		return securityManager;
	}

	public ModelManager getManager() {
		return manager;
	}
	

	public  I_DataLogicERP getDLERP(){
		return dlERP;
	}
	
	public  I_DataLogicSales getDLSales(){
		return dlSales;
	}
	
	public  I_DataLogic  getDataLogic(){
		return dlERP;
	}
	
	public  I_DataLogicReceipts getDLReceipts(){
		return dlReceipts;
	}
	
	public  I_DataLogicSystem getDLSystem(){
		return dlSystem;
	}
	
	public  I_DataLogicAdmin getDLAdmin(){
		return dlAdmin;
	}
	
	public  I_DataLogicCustomers getDLCustomer(){
		return dlCustomers;
	}

	public I_DataLogicSecurity getDlSecurity() {
		return dlSecurity;
	}

	@Override
	public boolean isCommandSupt(String commandName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void execCommand(I_ModelCommand mode) {
		// TODO Auto-generated method stub
	}


	public I_ProcessManager getProcessManager() {
		return processManager;
	}
	
	

}
